package com.example.springbootlearn.entity;

import cn.hutool.core.collection.ListUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author caiji Mr. Li
 * @date 2019/10/27 17:36
 */

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
    private int id;
    private String name;
    private String password;
    private Integer count;


    public static void main(String[] args) {
        User user1 = User.builder().id(2).name("afei").count(100).build();
        User user2 = User.builder().id(1).name("afei").count(110).build();
        User user3 = User.builder().id(3).name("afei2").count(120).build();
        User user4 = User.builder().id(5).name("afei1").count(130).build();
        User user5 = User.builder().id(4).name("afei1").count(140).build();

        List<User> list = ListUtil.of(user1, user2, user3, user4, user5);
        Map<String, Object> map = list.stream().collect(Collectors.toMap(User::getName, User::getCount, (x, y) -> x + "," + y));
        System.out.println(map);

        Map<Integer, List<User>> collect = list.stream().collect(Collectors.groupingBy(User::getId, LinkedHashMap::new, Collectors.toList()));
        System.out.println(collect);

        Map<String, Integer> map1 = list.stream().collect(Collectors.groupingBy(User::getName, LinkedHashMap::new, Collectors.summingInt(User::getCount)));
        System.out.println(map1);


        Map<String, Integer> hashmap = new HashMap<>();
        hashmap.put("aaa", 2);
        Integer merge = hashmap.merge("aaa", 1, Integer::sum);
        System.out.println("merge = " + merge);
        System.out.println(hashmap);


    }
}
